<?php

use Biz\cache\IkeaActivity\IkeaActivityCache;
use Biz\cache\wiki\Wiki;
use Biz\model\IkeaActivity\IkeaActivityJoinRecordModel;
use Biz\model\IkeaActivity\IkeaActivityWinnerListModel;
use Doraemon\model\AdmUserType;
use Doraemon\model\Phone;

class DrawAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currentUid = $this->uid;

        if ($currentUid == 0) {
            throw new Exception('参数错误');
        }
        $name = trim($this->getRequest()->getPost('name', ''));
        $phone = trim($this->getRequest()->getPost('phone', ''));
        $address = trim($this->getRequest()->getPost('address', ''));
        \Doraemon\tools\Log::info('IkeaActivity/Draw', compact('currentUid', 'name', 'phone', 'address'));

        //禁止重复提交
        $lock = Wiki::isLock('ikea_new_year_activity_'.$currentUid, 3);
        if ($lock) {
            throw new \Exception('操作过于频繁，请稍后再试');
        }
        //获取获奖名单
        $winner = IkeaActivityCache::getData();
         //查询是否已填写收货地址
         $hasAddress = IkeaActivityWinnerListModel::getByWhere([
            'uid' => $currentUid,
            'status' => IkeaActivityWinnerListModel::STATUS_NORMAL,
        ]);

        //判断活动是否结束以及是否填写收货地址 如果已经填写过收货地址 则不提示活动已结束，否则提示活动已结束
        if (date('Ymd') >= 20230121) {
            if (!empty($hasAddress)) { 
                throw new Exception('涉及新年假期，发货时间可能延迟，最晚2月3日前可发货完成');
            }else{
                throw new Exception('活动已结束！');
            }
        }
        //反垃圾处理
        $aMemberQualityInfo = AdmUserType::getByUid($currentUid);
        $aMemberPhone = Phone::getByUid($currentUid);
        if(AdmUserType::B1 == $aMemberQualityInfo['quality'] || trim($aMemberQualityInfo['identity_mark'] ?? '') == '批量注册' || 0 === strpos($aMemberPhone['hidden_phone'], '142')){
            throw new Exception('因违反好好住社区规范，暂时不能参与活动！');
        }
        \Doraemon\tools\Log::info('宜家活动反垃圾',compact('aMemberQualityInfo','aMemberPhone'));
        //查询是否完成全部任务
        $taskAllComplete = count(IkeaActivityJoinRecordModel::selectByWhere([
            'uid' => $currentUid,
            'type' => IkeaActivityJoinRecordModel::TYPE_TASK,
            'status' => IkeaActivityJoinRecordModel::STATUS_NORMAL,
        ]));
        if ($taskAllComplete < 5) {
            throw new Exception('还未点亮所有兔子灯，完成后可进行抽奖！');
        }
       

        
        $winnerCount = count($winner);

        //查看是否中奖
        if (in_array($currentUid, $winner)) {//查看是否在中奖名单里面
            if (empty($hasAddress)) { //中奖用户未填写收货地址
                if (!empty($name) && !empty($phone) && !empty($address)) {
                    IkeaActivityWinnerListModel::insertData([
                        'uid' => $currentUid,
                        'name' => $name,
                        'phone' => $phone,
                        'address' => $address,
                    ]);
                }
            } else {//中奖用户已填写收货地址 则toast提示
                throw new Exception('涉及新年假期，发货时间可能延迟，最晚2月3日前可发货完成');
            }
        }else {
            if ($winnerCount >= 300) {
                throw new Exception('很抱歉，奖品已领完，感谢您的参与！');
            } else {
                //放入缓存
                IkeaActivityCache::addData($currentUid);
            }
        }

        $this->setSuccess([], self::API_CODE_SUCCES);

    }
}